package com.xh3140.xwidget.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.Size;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.MutableLiveData;

import com.xh3140.xwidget.dialog.callback.ConfigParamsCallback;
import com.xh3140.xwidget.dialog.callback.ConfigViewCallback;
import com.xh3140.xwidget.dialog.listener.OnClickButtonListener;
import com.xh3140.xwidget.dialog.params.ContentParams;
import com.xh3140.xwidget.dialog.view.ContentView;
import com.xh3140.xwidget.dialog.view.FooterView;
import com.xh3140.xwidget.dialog.view.HeaderView;
import com.xh3140.xwidget.dialog.view.RootView;

/**
 * 内容对话框
 */
@SuppressWarnings("unused")
public final class ContentDialog extends BaseDialog<ContentView> {
  private ContentParams mContentParams;
  private OnClickButtonListener<ContentDialog> mOnClickButtonListener;

  @NonNull
  @Override
  protected RootView<ContentView> createRootView(@NonNull Context context) {
    final ContentDialog dialog = this;
    return new RootView<ContentView>(context) {
      @Override
      protected HeaderView createHeaderView(@NonNull Context context) {
        return new HeaderView(context).configView(mHeaderParams);
      }

      @Override
      protected ContentView createBodyView(@NonNull Context context) {
        return new ContentView(context).configView(mContentParams);
      }

      @Override
      protected FooterView createFooterView(@NonNull Context context) {
        return new FooterView(context).configView(dialog, mFooterParams, mOnClickButtonListener);
      }
    };
  }

  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ViewModel.getDefault(this, ViewModel.class).configContentDialogParams(this);
    return super.onCreateView(inflater, container, savedInstanceState);
  }

  /**
   * 数据状态保存
   * 由于屏幕方向旋转等情况会导致构建数据丢失，所以需要保存
   */
  public static final class ViewModel extends BaseDialog.ViewModel {
    private final MutableLiveData<ContentParams> mContentParamsData = new MutableLiveData<>();
    private final MutableLiveData<OnClickButtonListener<ContentDialog>> mOnClickButtonListenerData = new MutableLiveData<>();

    private void configContentDialogParams(@NonNull final ContentDialog dialog) {
      configBaseDialogParams(dialog);
      if (dialog.mContentParams == null) {
        dialog.mContentParams = mContentParamsData.getValue();
      } else {
        mContentParamsData.postValue(dialog.mContentParams);
      }
      if (dialog.mOnClickButtonListener == null) {
        dialog.mOnClickButtonListener = mOnClickButtonListenerData.getValue();
      } else {
        mOnClickButtonListenerData.postValue(dialog.mOnClickButtonListener);
      }
    }
  }

  /**
   * 内容对话框构建器
   * 设置内容对话框属性
   */
  public static final class Builder extends BaseDialog.Builder<ContentDialog, Builder> {
    private final ContentParams mContentParams;

    public Builder(@NonNull Context context) {
      super(context);
      mContentParams = new ContentParams(context);
      mDialogParams.minHeight = 0.2f;
    }

    public Builder(@NonNull FragmentActivity activity) {
      super(activity);
      mContentParams = new ContentParams(mDialogParams.context);
      mDialogParams.minHeight = 0.2f;
    }

    public Builder(@NonNull Fragment fragment) {
      super(fragment);
      mContentParams = new ContentParams(mDialogParams.context);
      mDialogParams.minHeight = 0.2f;
    }

    /**
     * 构建对话框
     * 这个方法应该在所有配置结束后调用
     */
    @Override
    public ContentDialog build() {
      final ContentDialog dialog = new ContentDialog();
      dialog.mDialogParams = mDialogParams;
      dialog.mHeaderParams = mHeaderParams;
      dialog.mFooterParams = mFooterParams;
      dialog.mContentParams = mContentParams;
      dialog.mOnClickButtonListener = mOnClickButtonListener;
      return dialog;
    }


    /**
     * 配置内容主体视图属性
     *
     * @param callback 配置回调
     */
    public Builder configContentParams(@NonNull ConfigParamsCallback<ContentParams> callback) {
      callback.callback(mContentParams);
      return this;
    }

    /**
     * 设置内容主体视图文本
     *
     * @param text 文本
     */
    public Builder setContentText(@Nullable CharSequence text) {
      mContentParams.text = text;
      return this;
    }

    /**
     * 设置内容主体视图文本字体大小 sp
     *
     * @param textSize 字体大小
     */
    public Builder setContentTextSize(float textSize) {
      mContentParams.textSize = textSize;
      return this;
    }

    /**
     * 设置内容主体视图文本字体颜色 argb
     *
     * @param textColor 字体颜色
     */
    public Builder setContentTextColor(@ColorInt int textColor) {
      mContentParams.textColor = textColor;
      return this;
    }

    /**
     * 设置内容主体视图文本字体样式
     *
     * @param typeface 字体样式
     */
    public Builder setContentTypeface(int typeface) {
      mContentParams.typeface = typeface;
      return this;
    }

    /**
     * 设置内容主体视图文本位置
     *
     * @param gravity 位置
     */
    public Builder setContentGravity(int gravity) {
      mContentParams.gravity = gravity;
      return this;
    }

    /**
     * 设置内容主体视图背景颜色 argb
     *
     * @param backgroundColor 背景颜色
     */
    public Builder setContentBackgroundColor(@ColorInt int backgroundColor) {
      mContentParams.backgroundColor = backgroundColor;
      return this;
    }

    /**
     * 设置内容主体视图内边距
     * 格式：[start, top, end, bottom] px
     *
     * @param padding 边距
     */
    public Builder setContentPadding(@Nullable @Size(4) int[] padding) {
      mContentParams.padding = padding;
      return this;
    }

    /**
     * 设置内容主体视图内边距
     *
     * @param padding 边距 px
     */
    public Builder setContentPadding(@Px int padding) {
      return setContentPadding(new int[]{padding, padding, padding, padding});
    }

    /**
     * 设置内容主体视图内边距
     *
     * @param left   左边距 px
     * @param top    上边距 px
     * @param right  右边距 px
     * @param bottom 下边距 px
     */
    public Builder setContentPadding(@Px int left, @Px int top, @Px int right, @Px int bottom) {
      return setContentPadding(new int[]{left, top, right, bottom});
    }

    /**
     * 设置内容主体视图配置回调
     * 拥有绝对控制权的回调，它在其他属性配置结束后回调
     * 一般情况不要使用它，除非提供的现有配置属性不满足需求
     *
     * @param callback 配置回调
     */
    public Builder setContentViewCallback(@Nullable ConfigViewCallback<ContentView> callback) {
      mContentParams.callback = callback;
      return this;
    }
  }
}
